package battleship.loop;

import battleship.Controller;
import battleship.PluginLoop;
import battleship.Setup;
import battleship.player.Player;
import battleship.view.Views;

public class Strike implements PluginLoop {
	
	// 0 : miss continue
	// 1 : error parsing
	// 2 : invalid spot
	// 3 : hit the enemy -> go again
	// 4 : hit the enemy + sunk boat
	
	public static final int MISS = 0;
	public static final int PARSE_ERROR = 1;
	public static final int INVALID = 2;
	public static final int HIT = 3;
	public static final int SUNK = 4;

	@Override
	public String loopGetFunction() {
		return "strike";
	}

	@Override
	public String loopGetDescription() {
		return "het commando om de vijand aan te vallen";
	}

	@Override
	public String loopGetOptions() {
		// TODO Auto-generated method stub
		return "[a1] coördinaten";
	}
	
	/**
	 * 
	 */
	@Override
	public int loopExecFunction(String[] arguments, Player player) {
		
		
		
		try {
			if(player.strike(arguments[0]))
			{
				return (Setup.getPlayer2(player).getField().getBoatAt(arguments[0]).getSunk())? SUNK:HIT;
				
			}
			
			Controller.getController().printBoard( false, Setup.getPlayer2(player).getField());
		} catch (IllegalArgumentException e) {
//			Controller.getController().displayError("Dit argument is fout", this);
			return PARSE_ERROR;
		} catch (MoveCollisionException e) {
//			Controller.getController().displayError("Er is al een boot op deze positie", this);
			return INVALID;
		} catch (OutOfSpaceException e) {
//			Controller.getController().displayError("Deze positie komt niet voor in het veld", this);
			return INVALID;
		}
		return MISS;
		
	}
	
	/**
	 * @param error the code that gets executed by the implementation
	 * @return false if it has to loop
	 */
	@Override
	public boolean loopErrorHandler(int error) {
		// 0 : miss continue
		// 1 : error parsing
		// 2 : invalid spot
		// 3 : hit the enemy -> go again
		// 4 : hit the enemy + sunk boat
		// 5 : hit the enemy + won game
		
		switch (error) {
		case 0:
			Controller.getController().displayNotification("Je hebt gemist!");
			return true;
		
		case 1:
			Controller.getController().displayNotification(loopGetFunction() + "\t" + loopGetOptions());
			return false;
		
		case 2:
			Controller.getController().displayNotification("Deze plaats is ongeldig");
			return false;
			
		case 3:
			Controller.getController().displayNotification("Je hebt geraakt!");
			return true;
		case 4:
			Controller.getController().displayNotification("Je hebt het schip gezonken!");
		default:
			return true;
		}
		
	}

	@Override
	public Views[] loopSupportedViews() {
		// TODO Auto-generated method stub
		return (new Views[] {Views.Console});
	}

	@Override
	public boolean acceptArguments() {
		// TODO Auto-generated method stub
		return true;
	}

}
